package com.huawei.updatesdk.support.pm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.updatesdk.a.b.c.b;
import com.huawei.updatesdk.fileprovider.UpdateSdkFileProvider;
import com.huawei.updatesdk.service.c.g;
import com.huawei.updatesdk.service.c.g.a;
import com.huawei.updatesdk.service.c.h;
import java.io.File;

public class PackageInstallerActivity extends Activity {
    private String a = "";
    private String b = "";
    private int c = -1;

    private Intent a(Context context, String str) {
        if (TextUtils.isEmpty(str) || str.contains("../")) {
            throw new IllegalArgumentException("getNomalInstallIntent: Not a standard path");
        }
        Intent intent = new Intent();
        intent.setAction("android.intent.action.INSTALL_PACKAGE");
        if (VERSION.SDK_INT >= 24) {
            intent.addFlags(1);
            intent.setData(UpdateSdkFileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + UpdateSdkFileProvider.AUTHORITIES_SUFFIX, new File(str)));
        } else {
            intent.setData(Uri.fromFile(new File(str)));
        }
        if (!(context instanceof Activity)) {
            intent.addFlags(268435456);
        }
        return intent;
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        int i3 = -10004;
        super.onActivityResult(i, i2, intent);
        if (1000 == i) {
            if (i2 == 0) {
                g.a(7, 0);
            } else {
                if (intent != null) {
                    i3 = intent.getIntExtra("android.intent.extra.INSTALL_RESULT", -10004);
                }
                String[] a = h.a();
                if (VERSION.SDK_INT < 24 && -3 == i3 && this.c < a.length - 1) {
                    a.a(this.b, -3, true);
                } else if (!(i3 == 0 || i3 == 1)) {
                    g.a(4, i3);
                }
            }
        }
        finish();
    }

    protected void onCreate(Bundle bundle) {
        requestWindowFeature(1);
        super.onCreate(bundle);
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            com.huawei.updatesdk.a.a.b.a.a.a.d("PackageInstallerActivity", "PackageInstallerActivity error intent");
            return;
        }
        b a = b.a(intent);
        this.a = a.a(com.huawei.fastengine.fastview.download.install.PackageInstallerActivity.INSTALL_PATH);
        this.b = a.a(com.huawei.fastengine.fastview.download.install.PackageInstallerActivity.INSTALL_PACKAGENAME);
        if (TextUtils.isEmpty(this.a)) {
            g.a(4, -3);
            finish();
            com.huawei.updatesdk.a.a.b.a.a.a.d("PackageInstallerActivity", "PackageInstallerActivity can not find filePath.");
            return;
        }
        this.c = a.a(com.huawei.fastengine.fastview.download.install.PackageInstallerActivity.INSTALL_CHANGE_PATH_TIMES, -1);
        try {
            intent = a(this, this.a);
            intent.putExtra("android.intent.extra.NOT_UNKNOWN_SOURCE", true);
            intent.putExtra("android.intent.extra.RETURN_RESULT", true);
            com.huawei.updatesdk.a.a.b.a.a.a.c("PackageInstallerActivity", " onCreate filePath:" + this.a + ",packageName:" + this.b + ",taskId:" + getTaskId());
            startActivityForResult(intent, 1000);
        } catch (Exception e) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("PackageInstallerActivity", "can not start install action");
            g.a(4, -2);
            finish();
        }
    }

    protected void onDestroy() {
        finishActivity(1000);
        super.onDestroy();
        com.huawei.updatesdk.a.a.b.a.a.a.c("PackageInstallerActivity", " onDestroy removeTaskId:" + this.a);
    }
}
